package com.gitee.wsl.io

import com.gitee.wsl.io.file.FileFilter
import com.gitee.wsl.io.file.FilenameFilter
import com.gitee.wsl.platform.AppDirs
import kotlinx.io.files.FileNotFoundException
import kotlinx.io.files.FileSystem
import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem

class Dir(
    override val path: Path,
    override val fileSystem: FileSystem = SystemFileSystem
): PathAble {

    val files:List<File> get() = fileSystem.list(path).filter { it.isPhysicalFile }.map {
        File(it,fileSystem)
    }

    val subDir:List<Dir> get() = fileSystem.list(path).filter { it.isDirectory(fileSystem) }.map { Dir(it) }

    val paths get() = fileSystem.list(path)

    override fun copyTo(destDir: Dir) = TODO()

    fun mkdir():Boolean = TODO()

    fun mkdirs():Boolean = TODO()

    fun listFiles(filter: FileFilter):List<File> = files.filter { filter.accept(it) }

    fun listFilesWithNameFilter(filter: FilenameFilter):List<File> = files.filter { filter.accept(this,it.name) }

    fun resolve(fileName: String): File = files.find { it.name == fileName }?:throw FileNotFoundException("$path/$fileName")

    override fun createIfNoExists() = path.createDirectoriesIfNoExists(fileSystem)

    fun deleteContents() = fileSystem.deleteContents(this.path)/*{
        subDir.forEach {
            it.deleteContents()
        }
        files.forEach {
            it.delete()
        }
    }*/
    companion object
}

fun Path.toDir(fileSystem: FileSystem = SystemFileSystem) = Dir(this,fileSystem)
